package com.mz361.modules.system.service.impl;

import com.mz361.common.data.PageSort;
import com.mz361.common.enums.StatusEnum;
import com.mz361.modules.system.domain.OrderUser;
import com.mz361.modules.system.repository.OrderUserRepository;
import com.mz361.modules.system.service.OrderUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author liaomch
 * @date 2024/05/07
 */
@Service
public class OrderUserServiceImpl implements OrderUserService {

    @Autowired
    private OrderUserRepository orderUserRepository;

    /**
     * 根据ID查询数据
     *
     * @param id 主键ID
     */
    @Override
    public OrderUser getById(Long id) {
        return orderUserRepository.findById(id).orElse(null);
    }

    /**
     * 获取分页列表数据
     *
     * @param example 查询实例
     * @return 返回分页数据
     */
    @Override
    public Page<OrderUser> getPageList(Example<OrderUser> example) {
        // 创建分页对象
        PageRequest page = PageSort.pageRequest();
        return orderUserRepository.findAll(example, page);
    }

    @Override
    public List<OrderUser> getOrderUsersByCompanyId(Long companyId) {
        return orderUserRepository.findAllByCompanyId(companyId);
    }

    @Override
    public List<OrderUser> getOrderUsersByMemeberId(Long memeberId) {
        return orderUserRepository.findAllByMemeberId(memeberId);
    }

    @Override
    public List<OrderUser> getOrderUsersByCompanyIdAndPackageId(Long companyId, Long packageId) {
        return orderUserRepository.findALLByCAndCompanyIdAndAndPackageId(companyId, packageId);
    }

    @Override
    public List<OrderUser> getOrderUsersByMemeberIdAndPackageId(Long memeberId, Long packageId) {
        return orderUserRepository.findALLByCAndMemeberIdAndPackageId(memeberId, packageId);
    }

    @Override
    public List<OrderUser> getOrderUsersByCompanyIdAndServiceName(Long companyId, String serviceName) {
        return orderUserRepository.findAllByCompanyIdAndServiceName(companyId, serviceName);
    }

    @Override
    public List<OrderUser> getOrderUsersByMemeberIdAndServiceName(Long memeberId, String serviceName) {
        return orderUserRepository.findAllByMemberIdAndServiceName(memeberId, serviceName);
    }

    /**
     * 保存数据
     *
     * @param orderUser 实体对象
     */
    @Override
    public OrderUser save(OrderUser orderUser) {
        return orderUserRepository.save(orderUser);
    }

    @Override
    public Boolean saveAll(List<OrderUser> orderUserList) {
        return orderUserRepository.saveAll(orderUserList).size() > 0;
    }

    /**
     * 状态(启用，冻结，删除)/批量状态处理
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateStatus(StatusEnum statusEnum, List<Long> idList) {
        return orderUserRepository.updateStatus(statusEnum.getCode(), idList) > 0;
    }
}